#include"MyPlayer.h"
#ifdef _DEBUG
#include<crtdbg.h>
#endif
int running=1;
#if USE_PLAY_AUDIO
static int useaudiothread=1;
#else
static int useaudiothread = 0;
#endif
static int usevideothread=1;
#if 0
#ifdef __MINGW32__
int WinMain(int argc,char*argv[]){
#else
int main(int argc,char*argv[]){
#endif    
#endif	
int myplayer(int argc,char*argv[]){	
    SDL_Thread*PlayThread=NULL;
    SDL_Thread*DecThread=NULL;
    const char*filename="test.mp4";
    if(argc==2){
        filename=argv[1];
    }
    if((SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO)==-1)) { 
        printf("Could not initialize SDL: %s.\n", SDL_GetError());
        exit(-1);
    }
    InitSDLMutx();
    av_log_set_level(AV_LOG_VERBOSE);
    if(InitFFMpeg(filename)!=0){
        exit(-1);
    }
    if(usevideothread) {
        PlayThread=SDL_CreateThread(SDL_VIDEO_Thread,NULL);
        if(PlayThread==NULL){
            printf("CreateThread Failed\n");
        }
    }
    if(useaudiothread) {
        InitAudioThread();
        SDL_PauseAudio(0);
    }
    DecThread=SDL_CreateThread(FF_DEC_Thread,NULL);
    if(DecThread==NULL){
       printf("CreateThread Failed\n");
    }
	
///////////////////////////////////////////
    while(running) {
      SDL_Event event={0};
      SDL_PollEvent(&event);
      if(event.type==SDL_QUIT){
         printf("Recv Quit Single\n"); 
         running=0;
      }
    }
    running=0;;
    if(DecThread)SDL_WaitThread(DecThread,NULL);
    if(PlayThread)SDL_WaitThread(PlayThread,NULL);
    UnInitSDLMutx();
    UnInitFFMpeg();
    SDL_CloseAudio();
    SDL_Quit();
#ifdef _DEBUG
	//_CrtSetBreakAlloc(453);
	_CrtDumpMemoryLeaks();
#endif
	return 0;
}